﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassList
{
    class MyList<T>
    {
        T[] arr = new T[4];

        public int Count
        {
            get;
            private set;
        }

        private void EnsureCapacity()
        {
            if (arr.Length == Count)
            {
                T[] newArr = new T[arr.Length * 2];
                Array.Copy(arr, newArr, arr.Length);
                arr = newArr;
            }
        }

        public void Add(T element)
        {
            EnsureCapacity();
            arr[Count] = element;
            Count++;
        }

        public T this[int index]
        {
            get
            {
                if (index >= Count)
                {
                    throw new IndexOutOfRangeException();
                }
                return arr[index];
            }
        }

        public void RemoveAt(int index)
        {
            if (index >= Count || index < 0)
            {
                throw new IndexOutOfRangeException();
            }
            for (int j = index + 1; j < Count; j++)
            {
                arr[j - 1] = arr[j];
            }
            Count--;
        }

        public void Remove(T element)
        {
            for (int i = 0; i < Count; i++)
            {
                if (element.Equals(arr[i]))
                {
                    RemoveAt(i);
                }
            }
        }

        public MyEnumerator<T> GetEnumerator()
        {
            return new MyEnumerator<T>(arr, Count);
        }
    }

    class MyEnumerator<T>
    {
        T[] arr;
        int count;
        int index = -1;

        public MyEnumerator(T[] arr, int count)
        {
            this.arr = arr;
            this.count = count;
        }

        public bool MoveNext()
        {
            index++;
            return index < count;
        }

        public T Current
        {
            get
            {
                return arr[index];
            }
        }
    }
}
